/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.asset.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import top.continew.admin.asset.mapper.ConsumableRecordsMapper;
import top.continew.admin.asset.model.entity.ConsumableRecordsDO;
import top.continew.admin.asset.model.query.ConsumableRecordsQuery;
import top.continew.admin.asset.model.req.ConsumableRecordsReq;
import top.continew.admin.asset.model.resp.ConsumableRecordsDetailResp;
import top.continew.admin.asset.model.resp.ConsumableRecordsResp;
import top.continew.admin.asset.service.ConsumableRecordsService;

/**
 * 耗材业务实现
 *
 * @author CharlieYan
 * @since 2025/05/07 17:38
 */
@Service
@RequiredArgsConstructor
public class ConsumableRecordsServiceImpl extends BaseServiceImpl<ConsumableRecordsMapper, ConsumableRecordsDO, ConsumableRecordsResp, ConsumableRecordsDetailResp, ConsumableRecordsQuery, ConsumableRecordsReq> implements ConsumableRecordsService {

    @Override
    public PageResp<ConsumableRecordsResp> page(ConsumableRecordsQuery query, PageQuery pageQuery) {
        QueryWrapper<ConsumableRecordsDO> queryWrapper = this.buildQueryWrapper(query);
        super.sort(queryWrapper, pageQuery);
        IPage<ConsumableRecordsDetailResp> page = baseMapper.selectConsumableRecordsPage(new Page<>(pageQuery
            .getPage(), pageQuery.getSize()), queryWrapper);
        PageResp<ConsumableRecordsResp> pageResp = PageResp.build(page, super.getListClass());
        pageResp.getList().forEach(this::fill);
        return pageResp;
    }

    @Override
    protected QueryWrapper<ConsumableRecordsDO> buildQueryWrapper(ConsumableRecordsQuery query) {
        String fuzzy = query.getFuzzy();
        return new QueryWrapper<ConsumableRecordsDO>().and(StrUtil.isNotBlank(fuzzy), q -> q.like("tt.name", fuzzy)
            .or()
            .like("tt.name", fuzzy)
            .or()
            .like("tt.category_name", fuzzy)
            .or()
            .like("tt.specification", fuzzy)
            .or()
            .like("tt.producer_name", fuzzy));
    }

}